package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldPayDetails;
import com.cleaningcloud.cld.domain.CldTrainPlan;
import com.cleaningcloud.cld.domain.CldTrainReceive;
import com.cleaningcloud.cld.domain.dto.CldProjectPostDto;
import com.cleaningcloud.cld.domain.dto.CldTrainReceiveDto;
import com.cleaningcloud.cld.domain.vo.CldProjectPostSetVo;
import com.cleaningcloud.cld.domain.vo.CldTrainReceiveVo;
import com.cleaningcloud.cld.mapper.CldProjectPostMapper;
import com.cleaningcloud.cld.mapper.CldTrainIecturerMapper;
import com.cleaningcloud.cld.mapper.CldTrainPlanMapper;
import com.cleaningcloud.cld.mapper.CldTrainReceiveMapper;
import com.cleaningcloud.cld.service.ICldPayDetailsService;
import com.cleaningcloud.cld.service.ICldTrainReceiveService;
import com.cleaningcloud.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 受训人信息Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-26
 */
@Service
public class CldTrainReceiveServiceImpl implements ICldTrainReceiveService {
    @Autowired
    private CldTrainReceiveMapper cldTrainReceiveMapper;

    @Autowired
    private CldTrainIecturerMapper cldTrainIecturerMapper;

    @Autowired
    private CldProjectPostMapper cldProjectPostMapper;

    @Autowired
    private CldTrainPlanMapper cldTrainPlanMapper;

    @Autowired
    private ICldPayDetailsService cldPayDetailsService;

    /**
     * 查询受训人信息
     *
     * @param trainReceiveId 受训人信息主键
     * @return 受训人信息
     */
    @Override
    public CldTrainReceive selectCldTrainReceiveByTrainReceiveId(Long trainReceiveId) {
        return cldTrainReceiveMapper.selectCldTrainReceiveByTrainReceiveId(trainReceiveId);
    }

    /**
     * 查询受训人信息列表
     *
     * @param cldTrainReceive 受训人信息
     * @return 受训人信息
     */
    @Override
    public List<CldTrainReceiveVo> selectCldTrainReceiveList(CldTrainReceiveDto cldTrainReceive) {
        return cldTrainReceiveMapper.selectCldTrainReceiveList(cldTrainReceive);
    }

    /**
     * 新增受训人信息
     *
     * @param cldTrainReceive 受训人信息
     * @return 结果
     */
    @Override
    public int insertCldTrainReceive(CldTrainReceive cldTrainReceive) {
        return cldTrainReceiveMapper.insertCldTrainReceive(cldTrainReceive);
    }

    /**
     * 修改受训人信息
     *
     * @param cldTrainReceive 受训人信息
     * @return 结果
     */
    @Override
    public int updateCldTrainReceive(CldTrainReceive cldTrainReceive) {
        CldTrainReceive trainReceive = cldTrainReceiveMapper.selectCldTrainReceiveByTrainReceiveId(cldTrainReceive.getTrainReceiveId());
        if(trainReceive == null){
            throw new RuntimeException("受训人信息查询失败");
        }
        if(trainReceive.getRecevieStatus() == 2 || trainReceive.getRecevieStatus() == 3){
            throw new RuntimeException("该记录已审核,请勿重复操作！");
        }
        if (cldTrainReceive.getRecevieStatus() == 2 || cldTrainReceive.getRecevieStatus() == 3) {
            cldTrainReceive.setRecevieTime(DateUtils.getNowDate());
            // 审核通过 计算培训费用薪酬明细
            if (cldTrainReceive.getRecevieStatus() == 2) {
                // 查询培训计划
                CldTrainPlan cldTrainPlan = cldTrainPlanMapper.selectCldTrainPlanByTrainPlanId(cldTrainReceive.getTrainPlanId());
                if (cldTrainPlan != null) {
                    CldPayDetails cldPayDetails = new CldPayDetails();
                    cldPayDetails.setPayType(8L);
                    cldPayDetails.setStaffId(cldTrainPlan.getTrainIecturerId());
                    cldPayDetails.setBusinessId(cldTrainReceive.getTrainReceiveId());
                    // 查询培训讲师项目岗位
                    CldProjectPostDto postQuery = new CldProjectPostDto();
                    postQuery.setStaffId(cldTrainPlan.getTrainIecturerId());
                    postQuery.setIsMasterDuits(1L);
                    List<CldProjectPostSetVo> cldProjectPostSetVos = cldProjectPostMapper.selectCldProjectPostList(postQuery);
                    if (!cldProjectPostSetVos.isEmpty()) {
                        cldPayDetails.setProjectId(cldProjectPostSetVos.get(0).getProjectId());
                        cldPayDetails.setProjectPostId(cldProjectPostSetVos.get(0).getProjectPostName());
                    }
                    cldPayDetails.setMoney(cldTrainPlan.getMoney());
                    cldPayDetails.setIsAlgorithm(1L);
                    cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                }
            }
        }
        if (cldTrainReceive.getRecevieStatus() == 1) {
            cldTrainReceive.setCompleteTime(DateUtils.getNowDate());
        }
        return cldTrainReceiveMapper.updateCldTrainReceive(cldTrainReceive);
    }

    /**
     * 批量删除受训人信息
     *
     * @param trainReceiveIds 需要删除的受训人信息主键
     * @return 结果
     */
    @Override
    public int deleteCldTrainReceiveByTrainReceiveIds(Long[] trainReceiveIds) {
        return cldTrainReceiveMapper.deleteCldTrainReceiveByTrainReceiveIds(trainReceiveIds);
    }

    /**
     * 删除受训人信息信息
     *
     * @param trainReceiveId 受训人信息主键
     * @return 结果
     */
    @Override
    public int deleteCldTrainReceiveByTrainReceiveId(Long trainReceiveId) {
        return cldTrainReceiveMapper.deleteCldTrainReceiveByTrainReceiveId(trainReceiveId);
    }
}
